package com.lll.simple.rpc.protocol.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @Author: liull
 * @Description: 空闲检测Handler如果服务端 60s 没未读到数据，就会回调 channelIdle() 方法，此时我们进行连接关闭，避免资源浪费。
 * @Date: Created in 15:40 2021/2/7
 * @Modified By:
 */
@Slf4j
public class RpcIdleStateHandler extends IdleStateHandler {
    /**
     * 60s没有读取到客户端的数据就关掉连接
     */
    public RpcIdleStateHandler() {
        super(60, 0, 0, TimeUnit.SECONDS);
    }

    @Override
    protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
        log.info("正在关闭客户端:{}连接通道",ctx.channel().remoteAddress());
        ctx.channel().close();
    }
}
